package org.millioncall.yueyoga.admin.dao.impl;

import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.GroupsDAO;
import org.millioncall.yueyoga.admin.model.Groups;
import org.springframework.stereotype.Repository;

@Repository
public class GroupsDAOImpl extends BaseDao implements GroupsDAO {

	private static final String SAVE_GROUPS = "saveGroups";
	private static final String UPDATE_GROUPS_BY_ID = "updateGroupsById";
	private static final String DELETE_GROUPS_BY_ID = "deleteGroupsById";
	private static final String DELETE_MEMBERGROUPS_BY_GROUPID = "deleteMemberGroupsByGroupId";
	private static final String FIND_GROUPS_BY_ID = "findGroupsById";
	private static final String FIND_GROUPS_BY_HALLCODE = "findGroupsByhallCode";
	private static final String FIND_GROUPS_BY_NAME = "findGroupsByName";
	private static final String FIND_MEMBER_GROUPS_BY_CDTS = "findMemberGroupsByCdts";

	@Override
	public int saveGroups(Map<String, Object> map) {
		return this.save(SAVE_GROUPS, map);
	}

	@Override
	public int deleteGroupsById(Integer id) {
		int m = this.deleteById(DELETE_GROUPS_BY_ID, id);
		int x = this.deleteMemberGroupsByGroupId(id);
		return m + x;
	}

	@Override
	public int updateGroupsById(Map<String, Object> map) {
		return this.update(UPDATE_GROUPS_BY_ID, map);
	}

	@Override
	public Groups findGroupsById(Integer id) {
		return this.getPagableSqlSessionTemplate().selectOne(FIND_GROUPS_BY_ID,
				id);
	}

	@Override
	public List<Groups> findGroupsByOrganizationId(String organizationId) {
		return this.getPagableSqlSessionTemplate().selectList(
				FIND_GROUPS_BY_HALLCODE, organizationId);
	}

	@Override
	public int findGroupsByName(String name) {
		return this.getPagableSqlSessionTemplate().selectOne(
				FIND_GROUPS_BY_NAME, name);
	}

	@Override
	public PaginatedList<Groups> findMemberGroupsByCdts(
			Map<String, Object> map, int start, int limit) {
		PreparedQuery<Groups> pq = this.getPagableSqlSessionTemplate().findAll(
				FIND_MEMBER_GROUPS_BY_CDTS, map);
		PaginatedList<Groups> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int deleteMemberGroupsByGroupId(Integer groupId) {
		return this.getPagableSqlSessionTemplate().delete(
				DELETE_MEMBERGROUPS_BY_GROUPID, groupId);
	}
}
